// pages/feedback/feedback.js
Page({
  data: {
    tabs: [
      { id: 0, value: '体验问题', isActive: true },
      { id: 1, value: '商品/商家投诉', isActive: false }],
    picArr: [],
    inputValue: ''
  },
  onClear(e) {
    const { index } = e.currentTarget.dataset
    console.log(index);
    const { picArr } = this.data
    picArr.splice(index, 1)
    this.setData({ picArr })
  },
  onAddPic() {
    wx.chooseImage({
      count: 9,// 最多张数
      sizeType: ['original', 'compressed'],// 大小 原图 压缩
      sourceType: ['album', 'camera'],// 来源 相册 相机
      success: (result) => {
        console.log(result);
        this.setData({
          picArr: [...this.data.picArr, ...result.tempFilePaths]
        })
      },
      fail: () => { },
      complete: () => { }
    });
  },
  onTabsItemChange(e) {
    const { index } = e.detail
    let { tabs } = this.data
    tabs.forEach((v, i) => {
      i === index ? v.isActive = true : v.isActive = false
    });
    this.setData({
      tabs
    })
  },
  onLoad: function (options) {

  },
  onInput(e) {
    this.setData({ inputValue: e.detail.value })
  },
  // 外网图片
  uploadPic: [],
  submitBtn() {
    console.log('submit')
    const { inputValue, picArr } = this.data
    if (!inputValue.trim()) {
      wx.showToast({
        title: '请正确输入后提交',
        icon: 'none',
        duration: 1500,
        mask: true,
        success: (result) => {

        },
        fail: () => { }
      });
      return
    }
    wx.showLoading({
      title: "正在上传",
      mask: true,
      success: (result) => {

      },
      fail: () => { },
      complete: () => { }
    });
    if (picArr.length !== 0) {
      picArr.forEach(v => {
        // 图传接口有问题
        wx.uploadFile({//只能上传一个,所以外面需要forEach
          url: 'https://images.ac.cn/Home/Index/UploadAction/',
          filePath: v,
          name: "file",
          formData: {},
          success: (result) => {
            console.log(result)
            let url = JSON.parse(result.data).url
            this.uploadPic.push(url)
            console.log(this.uploadPic);
            if (i === picArr.length - 1) {
              this.setData({
                picArr: [],
                inputValue: ''
              })
              wx.hideLoading();
              wx.navigateBack({
                delta: 1
              });
            }
          },
          fail: () => { },
          complete: () => { }
        });
      })

    } else {
      wx.showToast({
        title: '提交成功',
        icon: 'success',
        duration: 1500,
        mask: true,
        success: (result) => {
          wx.hideLoading();
          wx.navigateBack({
            delta: 1
          });
        },
        fail: () => { }
      });
    }
  }
})